package com.rimi.controller;

import com.rimi.commons.CommonResult;
import com.rimi.model.dto.OrderGoodsDto;
import com.rimi.service.OrderService;
import com.rimi.util.GetTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author luka
 * @version V1.0
 * @date 2021 2021/7/7 0007 11:49
 */
@RestController
@Api(description = "订单管理",tags = "订单管理")
@CrossOrigin
public class OrderController {

    @Autowired
    OrderService orderService;

    @Transactional
    @PostMapping("/user/addOrders")
    @ApiOperation("添加订单")
    public CommonResult addUser(Integer goodId,Integer goodQuantity) {
        return orderService.addOrder(GetTokenUtil.getUser().getUserId(),goodId,goodQuantity);
    }

    @GetMapping("/user/queryOrders")
    @ApiOperation("查询订单")
    public CommonResult<List<OrderGoodsDto>> orderGoods() {
        String function = "";
        List<OrderGoodsDto> goodsListDtos = orderService.orderGoods(GetTokenUtil.getUser().getUserId(),function);
        return CommonResult.onSuccess(goodsListDtos);
    }

    @GetMapping("/user/readyToPay")
    @ApiOperation("查询待付款订单")
    public CommonResult<List<OrderGoodsDto>> readyToPayOrderGoods() {
        String function = "readyToPay";
        List<OrderGoodsDto> goodsListDtos = orderService.orderGoods(GetTokenUtil.getUser().getUserId(),function);
        return CommonResult.onSuccess(goodsListDtos);
    }

    @GetMapping("/user/readyToReceive")
    @ApiOperation("查询待收货订单")
    public CommonResult<List<OrderGoodsDto>> readyToReceiveOrderGoods() {
        String function = "readyToReceive";
        List<OrderGoodsDto> goodsListDtos = orderService.orderGoods(GetTokenUtil.getUser().getUserId(),function);
        return CommonResult.onSuccess(goodsListDtos);
    }

    @GetMapping("/user/readyToEvaluate")
    @ApiOperation("查询待评价订单")
    public CommonResult<List<OrderGoodsDto>> readyToEvaluateOrderGoods() {
        String function = "readyToEvaluate";
        List<OrderGoodsDto> goodsListDtos = orderService.orderGoods(GetTokenUtil.getUser().getUserId(),function);
        return CommonResult.onSuccess(goodsListDtos);
    }

    @GetMapping("/user/AlreadyDone")
    @ApiOperation("查询已完成订单")
    public CommonResult<List<OrderGoodsDto>> AlreadyDoneOrderGoods() {
        String function = "AlreadyDone";
        List<OrderGoodsDto> goodsListDtos = orderService.orderGoods(GetTokenUtil.getUser().getUserId(),function);
        return CommonResult.onSuccess(goodsListDtos);
    }

    @Transactional
    @PutMapping("/user/cancelOrder")
    @ApiOperation("取消订单")
    public CommonResult cancelOrder(Integer orderId) {
        return orderService.cancelOrder(orderId);
    }

    @PutMapping("/user/received")
    @ApiOperation("已收货,订单状态变为待评价")
    public CommonResult received(Integer orderId) {
        return orderService.received(orderId);
    }

    @PutMapping("/user/evaluated")
    @ApiOperation("已评价,订单状态变为已完成")
    public CommonResult evaluated(Integer orderId) {
        return orderService.evaluated(orderId);
    }

}
